package utility;

public class Vector {

	public double[] elements;
	
	public Vector(double[] elements) {
		if(elements == null) {
			throw new IllegalArgumentException("Vector cannot be null.");
		}
		this.elements = elements.clone();
	}
	
	public Vector minus(Vector other) {
		if(elements.length != other.elements.length) {
			throw new IllegalArgumentException("Vectors must be of equal dimensions.");
		}
		double[] difference = new double[elements.length];
		for(int i = 0; i < other.elements.length; i++) {
			difference[i] = elements[i] - other.elements[i];
		}
		return new Vector(difference);
	}
	
	public double dotProduct(Vector other) {
		if(elements.length != other.elements.length) {
			throw new IllegalArgumentException("Vectors must be of equal dimensions.");
		}
		double product = 0;
		for(int i = 0; i < elements.length; i++) {
			product += elements[i] * other.elements[i];
		}
		return product;
	}
	
	public Vector3 toVector3() {
		if(elements.length != 3) {
			throw new IllegalArgumentException("Must have 3 elements");
		}
		return new Vector3(new double[]{elements[0], elements[1], elements[2]});
	}
	
	public String toString() {
		String vectorStr = "(";
		for(int i = 0; i < elements.length - 1; i++) {
			vectorStr += elements[i] + ", ";
		}
		vectorStr += elements[elements.length - 1] + ")";
		return vectorStr;
	}
	
	public boolean equals(Object that) {
		if(that == null) {
			return false;
		}
		if(this == that) {
			return true;
		}
		if(!(that instanceof Vector)) {
			return false;
		}
		Vector thatVector = (Vector)that;
		boolean equal = true;
		for(int i = 0; i < elements.length; i++) {
			if(this.elements[i] != thatVector.elements[i]) {
				equal = false;
			}
		}
		return equal;
	}
	
	public int hashCode() {
		int prime = 23;
		int hash = 1;
		for(int i = 0; i < elements.length; i++) {
			hash += (int)((i + 1) * prime * elements[i]);
		}
		return hash;
	}
}
